package com.sharp.wechat.controller;

import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.wechat.service.auth.WechatAuthJdkService;
import com.sharp.wechat.service.auth.WechatAuthService;
import com.sharp.wechat.vo.auth.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Title: WechatAuthController
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/6/19 10:50
 */
@Slf4j
@RestController
@RequestMapping(value = "/wechat/auth")
@Api(value = "微信-公众号鉴权", tags = "微信-公众号鉴权")
public class WechatAuthController {

    @Resource
    private WechatAuthService wechatAuthService;
    @Resource
    private WechatAuthJdkService wechatAuthJdkService;


    @ApiOperation(value = "获取OAuth2登录url", notes = "获取OAuth2登录url")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "redirectUrl", value = "重定向url", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "state", value = "状态")})
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Response<AuthUrlRes>> getAuthUrl(AuthUrlReq req) {
        AuthUrlRes res = wechatAuthService.getOAuthUrl(req);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "使用鉴权码登录", notes = "使用鉴权码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "code", value = "鉴权码", required = true)})
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<Response<AuthRes>> getAuthUrl(AuthReq req) {
        AuthRes res = wechatAuthService.login(req);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "获取js sdk鉴权信息", notes = "获取js sdk鉴权信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "baseUrl", value = "应用域名", required = true)})
    @RequestMapping(value = "/jsApi", method = RequestMethod.GET)
    public ResponseEntity<Response<AuthJdkApiRes>> getJsApiTicket(AuthJdkApiReq req) {
        AuthJdkApiRes res = wechatAuthJdkService.getAuthJdkApiSignature(req);
        return ResponseHandler.generateResponse(res);
    }

}
